package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.server.mapper.QuestionsMapper;
import com.tanhua.server.pojo.Options;
import com.tanhua.server.pojo.Questions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionsService {

    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private OptionsService optionsService;


    /**
     * 根据问卷星级查询对应的问题
     *
     * @param star
     * @return
     */
    public List<Questions> queryQuentionsByStar(Integer star) {
        //根据星别查询数据库获取题目集合
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("star", star);
        //获取对应的问题的集合
        List<Questions> questionsList = questionsMapper.selectList(queryWrapper);

        //根据问题id查询选项
        for (Questions questions : questionsList) {
            List<Options> optionsList = optionsService.queryOptionsById(questions.getId());

            questions.setOptions(optionsList);
        }

        return questionsList;
    }

    /**
     *  根据问题id获取问题对象
     * @param questionId
     * @return
     */
    public Questions queryById(String questionId) {
        return questionsMapper.selectById(questionId);
    }
}
